package com.jpress.utils;

import java.util.ArrayList;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.SimpleEmail;

public class EmailUtils {

	public enum TipoEmail {
		EMAIL_SIMPLES, EMAIL_HTML, EMAIL_ANEXO
	}

	public EmailUtils() {

	}

	public static void enviarEmailTexto(String assunto, String mensagem,
			ArrayList<String> destinatarios, ArrayList<String> copiasCarbono,
			ArrayList<String> copiasOcultas) throws EmailException {
		SimpleEmail email = (SimpleEmail) emailFactory(TipoEmail.EMAIL_SIMPLES);
		for (String destinatario : destinatarios) {
			email.addTo(destinatario);
		}
		if (copiasCarbono != null) {
			for (String copiaCarbono : copiasCarbono) {
				email.addCc(copiaCarbono);
			}
		}
		if (copiasOcultas != null) {
			for (String copiaOculta : copiasOcultas) {
				email.addBcc(copiaOculta);
			}
		}
		email.setSubject(assunto);
		email.setMsg(mensagem);
		email.send();
	}

	public static void enviarEmailHTML(String assunto, String textoMensagem, 
			ArrayList<String> destinatarios, 
			ArrayList<String> copiasCarbono, 
			ArrayList<String> copiasOcultas) throws EmailException{
		HtmlEmail email = (HtmlEmail) emailFactory(TipoEmail.EMAIL_HTML);
		email.setSubject(assunto);
		for (String destinatario : destinatarios) {
			email.addTo(destinatario);
		}
		if (copiasCarbono != null) {
			for (String copiaCarbono : copiasCarbono) {
				email.addCc(copiaCarbono);
			}
		}
		if (copiasOcultas != null) {
			for (String copiaOculta : copiasOcultas) {
				email.addBcc(copiaOculta);
			}
		}

		email.setHtmlMsg(textoMensagem);
		email.send();
	}

	public void enviarEmailAnexo() {

	}

	public static Email emailFactory(TipoEmail tipo) throws EmailException {
		Email email;
		switch (tipo) {
		case EMAIL_SIMPLES:
			email = new SimpleEmail();
			break;
		case EMAIL_HTML:
			email = new HtmlEmail();
			break;
		default:
			email = new SimpleEmail();
		}
		email.setHostName("smtp.gmail.com");
		email.setFrom("jpress.noreply@gmail.com");
		email.setAuthentication("jpress.noreply@gmail.com", "jpress123456");
		email.setSmtpPort(465);
		email.setSSL(true);
		email.setTLS(true);
		return email;
	}

}
